package com.neusoft.nepb.controller;

import com.neusoft.nepb.common.AlertUtils;
import com.neusoft.nepb.common.FileUtils;
import com.neusoft.nepb.common.SceneUtils;
import com.neusoft.nepb.entity.Data;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

public class UploadDataViewController {
    @FXML
    ImageView imageView1,imageView2;
    @FXML
    private CheckBox option1,option2,option3,option4,option5,option6;
    @FXML
    private TextField txt_info;

    //初始化checkbox
    @FXML
    private void handleOption1(ActionEvent event){
        if(option1.isSelected()) {
            option2.setSelected(false);
            option3.setSelected(false);
            option4.setSelected(false);
            option5.setSelected(false);
            option6.setSelected(false);
        }
    }
    @FXML
    private void handleOption2(ActionEvent event){
        if(option2.isSelected()) {
            option1.setSelected(false);
            option3.setSelected(false);
            option4.setSelected(false);
            option5.setSelected(false);
            option6.setSelected(false);
        }
    }
    @FXML
    private void handleOption3(ActionEvent event){
        if(option3.isSelected()) {
            option2.setSelected(false);
            option1.setSelected(false);
            option4.setSelected(false);
            option5.setSelected(false);
            option6.setSelected(false);
        }
    }
    @FXML
    private void handleOption4(ActionEvent event){
        if(option4.isSelected()) {
            option2.setSelected(false);
            option3.setSelected(false);
            option1.setSelected(false);
            option5.setSelected(false);
            option6.setSelected(false);
        }
    }
    @FXML
    private void handleOption5(ActionEvent event){
        if(option5.isSelected()) {
            option2.setSelected(false);
            option3.setSelected(false);
            option4.setSelected(false);
            option1.setSelected(false);
            option6.setSelected(false);
        }
    }
    @FXML
    private void handleOption6(ActionEvent event){
        if(option6.isSelected()) {
            option2.setSelected(false);
            option3.setSelected(false);
            option4.setSelected(false);
            option5.setSelected(false);
            option1.setSelected(false);
        }
    }

    public void initialize() {
        //初始化图片
        Image image1 = new Image("/com/neusoft/nepb/image/level.png");
        imageView1.setImage(image1);
        imageView1.setFitWidth(496);
        imageView1.setFitHeight(296);
        imageView1.setPreserveRatio(false);
        SceneUtils.setImage(imageView2,"/com/neusoft/nepb/image/sail.jpg");
    }

    public void uploadNetData() throws IOException {
        String degree;
        String info;
        String province;
        String city;
        String district;
        String detailAddress;
        Date time;
        //判断污染等级
        if (option1.isSelected()) {
            degree = "一";
        } else if (option2.isSelected()) {
            degree = "二";
        } else if (option3.isSelected()) {
            degree = "三";
        }else if (option4.isSelected()) {
            degree = "四";
        }else if (option5.isSelected()) {
            degree = "五";
        }else if (option6.isSelected()) {
            degree = "六";
        }else {
            AlertUtils.showDialog("错误！", "请选择污染等级");
            return;
        }
        info = txt_info.getText();
        time = new Date();
        //读取文件中的信息
        String filePath = "location.txt";
        try(BufferedReader br = new BufferedReader(new FileReader(filePath))){
            province = br.readLine();
            city = br.readLine();
            district = br.readLine();
            detailAddress = br.readLine();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //
        Data data = new Data(degree,time,province,city,district,detailAddress,info);
        Map<Date,Data> finalDataMap = FileUtils.readMapObject("finalMap.txt");
        finalDataMap.put(data.getTime(),data);
        FileUtils.writeMapObject("finalMap.txt",finalDataMap);
        AlertUtils.showDialog("上传数据","上传成功");
    }

    public void back(ActionEvent event) throws IOException {
        SceneUtils.switchTo(event,"/com/neusoft/nepb/view/SeclectNetView.fxml");
    }
}
